package com.shengqi.xinnongren.work.ui.user.presenter;

import android.provider.ContactsContract;

import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.md5.MakeToken;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.TimeStampBean;
import com.shengqi.xinnongren.work.ui.user.view.LoginView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xuyougen on 2018/6/5.
 */

public class LoginPresenter extends BasePresenter<LoginView> {

    private String localToken;
    private String iv, key; // 加解密数据使用

    public void doLogin(final String phone, final String pwd) {
        getView().showLoadingView();
        final JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getTimeStamps(requestBody).flatMap((Function<HttpRespond<TimeStampBean>, ObservableSource<HttpRespond>>) timeStampBeanHttpRespond -> {
            LogUtil.e(timeStampBeanHttpRespond.data.toString());

            Map<String, Object> maps = new HashMap<>();
            maps.put("mobile", phone);
            maps.put("passWord", SafeUtils.getEncryptPwd(pwd));
            maps.put("timeId", timeStampBeanHttpRespond.data.id);
            iv = MakeToken.getIv(pwd, timeStampBeanHttpRespond.data.id);
            key = MakeToken.getKey(phone, timeStampBeanHttpRespond.data.val);

            localToken = MakeToken.getToken(phone, pwd, timeStampBeanHttpRespond.data.id, timeStampBeanHttpRespond.data.val);
            JSONObject data = Constants.getJsonData(maps, localToken);

            RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), data.toString());
            return RetrofitHelper.getInstance().getService().login(requestBody1);
        }), httpRespond -> {
            getView().hideLoadingView();
            if (httpRespond.result == 1) {
                getView().loginRes(httpRespond);
            } else
                getView().showRequestFailed(httpRespond.message);
        });
    }

    public void WeChatLogin(String uid, String name, String headUrl) {

        final JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getTimeStamps(requestBody).flatMap((Function<HttpRespond<TimeStampBean>, ObservableSource<HttpRespond<String>>>) timeStampBeanHttpRespond -> {
            LogUtil.e(timeStampBeanHttpRespond.data.toString());

            Map<String, Object> map = new HashMap<>();
            map.put("openId", uid);
            map.put("nickName", name);
            map.put("headUrl", headUrl);
            map.put("timeId", timeStampBeanHttpRespond.data.id);

            iv = MakeToken.getIv("123456", timeStampBeanHttpRespond.data.id);
            key = MakeToken.getKey(uid, timeStampBeanHttpRespond.data.val);
            localToken = MakeToken.getToken(uid, "123456", timeStampBeanHttpRespond.data.id, timeStampBeanHttpRespond.data.val);

            JSONObject jsonData1 = Constants.getJsonData(map, localToken);
            RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), jsonData1.toString());
            return RetrofitHelper.getInstance().getService().WeChatLogin(requestBody1);
        }), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showWeChatLoginRes(respond.data);
            } else
                getView().showRequestFailed(respond.message);
        });

    }

    public String getLocalToken() {
        return localToken;
    }

    public String getIv() {
        return iv;
    }

    public String getKey() {
        return key;
    }


    public void getALiLoginData() {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("targetId", "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), Constants.getJsonData(map, null).toString());
        addTask(RetrofitHelper.getInstance().getService().getALiLoginData(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showALiLoginData(respond.data);
            } else
                getView().onRequestFailed(respond.message);
        });
    }

    public void ALiLogin(String user_id, String auth_code) {
        final JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getTimeStamps(requestBody).flatMap((Function<HttpRespond<TimeStampBean>, ObservableSource<HttpRespond<String>>>) timeStampBeanHttpRespond -> {
            LogUtil.e(timeStampBeanHttpRespond.data.toString());

            Map<String, Object> map = new HashMap<>();
            map.put("authCode", auth_code);
            map.put("timeId", timeStampBeanHttpRespond.data.id);

            iv = MakeToken.getIv("123456", timeStampBeanHttpRespond.data.id);
            key = MakeToken.getKey(user_id, timeStampBeanHttpRespond.data.val);
            localToken = MakeToken.getToken(user_id, "123456", timeStampBeanHttpRespond.data.id, timeStampBeanHttpRespond.data.val);

            JSONObject jsonData1 = Constants.getJsonData(map, localToken);
            RequestBody requestBody1 = RequestBody.create(MediaType.parse("application/json"), jsonData1.toString());
            return RetrofitHelper.getInstance().getService().ALiLogin(requestBody1);
        }), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                getView().showALiLoginRes(respond.data);
            } else
                getView().showRequestFailed(respond.message);
        });
    }
}
